<?php
namespace app\common\libs;

use \app\common\model\Config;

class WeixinHelper
{
	/*
	 * 获取微信token
	 */
	static function getAccessToken() {
	    $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential';
	    $url .= '&appid='.config('weixin.appid').'&secret='.config('weixin.appsecret');
	    //APPID 应用id ; SECRET 应用密码, 在公众号平台-->开发->基本设置中查看
	    $tmp = \CURLSend($url, 'get'); //调用自定函数访问接口地址获取用户信息, 返回值为: json数据格式
	    $obj = json_decode($tmp, true); //json数据转成对象
	    
	    if(isset($obj['errcode'])){
	    	trace($obj['errmsg'],'error');
	    	return ['access_token'=>'', 'expires_in'=>''];
	    }

	    $data = array(
	        'access_token' => $obj['access_token'],
	        'expires_in' => (time() + $obj['expires_in']), //expires_in7200秒,加当前时间得到失效具体时间
	    );

	    return $data;
	}

	static function getWXUserInfo($token, $FromUserName) {
		$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$token;
		$url .= "&openid=".$FromUserName."&lang=zh_CN";

		$tmpJson = CURLSend($url, 'get');
        
        $info = json_decode($tmpJson, true);

        if(isset($info['errcode'])){
	    	trace($info['errmsg'],'error');
	    	return [];
	    }

        return $info;
	}

	/*
	 * {
		     "kf_account" : "test1@test",
		     "nickname" : "客服1",
		     "password" : "pswmd5",
		}
	 */
	static function addKfaccount($account, $token) {
		$url = 'https://api.weixin.qq.com/customservice/kfaccount/add?access_token='.$token;

		return \curlJson($url, $account);
	}

	/*
	 * 发送客服消息
	 */
	static function kfMessage($message, $token) {
		$url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token='.$token;

		return \curlJson($url, $message);
	}

	static function addTmpImg($imgUrl, $token) {
		$url = 'https://api.weixin.qq.com/cgi-bin/media/upload?access_token='.$token.'&type=image';
		$file = \GrabImage($imgUrl);

		$result = json_decode(upload_file_to_cdn($file, $url), true);

		return isset($result['media_id']) ? $result['media_id'] : '';
	}

	/*
	 * 自定义菜单
	 */
	static function customOrder($json_data, $token) {
		$url = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.$token;

		return json_decode(\CURLSend($url,'post',$json_data), true);
	}

	/*
	 * 取消自定义菜单
	 */
	static function cancelCustomOrder($token) {
		$url = 'https://api.weixin.qq.com/cgi-bin/menu/delete?access_token='.$token;

		return json_decode(\CURLSend($url), true);
	}

	/*
	 * 发送模板通知消息
	 */
	static function sendTplMessage($json_data, $token) {
		$url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$token;

		return json_decode(\CURLSend($url,'post',$json_data), true);
	}

	/*
	 * 网页授权获取访问code
	 */
	static function getCodeForWeb($appid, $callback, $scope='snsapi_base') {
		$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid;
		$url .= '&redirect_uri='.urlencode($callback);
		$url .= '&response_type=code&scope='.$scope.'&state=STATE#wechat_redirect';

		return $url;
	}

	/*
	 * 通过第一步获取到的网页code,来获取用户访问access_token
	 */
	static function getWebAccessTokenByCode($appid, $secret, $code) {
		$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid;
		$url .= '&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';

		return json_decode(\CURLSend($url), true);
	}

	/*
	 *	获取微信永久素材列表
	 */
	static function getMaterialList($token, $type='news', $offset=0, $count=100) {
		$url = 'https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token='.$token;
		return curlJson($url, ['type'=>$type, 'offset'=>$offset, 'count'=>$count]);
	}
}